package com.itheima.service.impl;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.constant.RedisMessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.*;
import com.itheima.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;

    /**
     * 用户端-新增预约
     */
    @Transactional
    public Result submitOrder(OrderDTO orderDTO) {
        String code = (String)redisTemplate.opsForValue().get(RedisMessageConstant.SENDTYPE_ORDER + "-" + orderDTO.getTelephone());
        //验证码有没有过期
        if(code == null || code.equals("")){
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }
        //验证码是否一致
        if(!orderDTO.getValidateCode().equals(code)){
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String orderDate = sdf.format(orderDTO.getOrderDate());
        OrderSetting orderSetting = orderSettingDao.getByOrderDate(orderDate);
        if(orderSetting != null && orderSetting.getNumber() != 0){
            //预约日期 设置了预约人数
            if(orderSetting.getReservations() < orderSetting.getNumber()){
                //预约日期没有约满，可以预约，判断用户有没有注册，如果没有就先注册会员
                Member member = memberDao.getByTelephone(orderDTO.getTelephone());
                if(member == null){
                    // 注册会员
                    member = new Member();
                    member.setName(orderDTO.getName());
                    member.setSex(orderDTO.getSex());
                    member.setIdCard(orderDTO.getIdCard());
                    member.setRegTime(orderDTO.getOrderDate());
                    member.setPhoneNumber(orderDTO.getTelephone());
                    memberDao.insert(member);
                }
                Order order = orderDao.getByMemberIdAndDate(member.getId(), orderDate, orderDTO.getSetmealId());
                if(order == null){
                    // 没有预约过
                    order = new Order();
                    order.setMemberId(member.getId());
                    order.setOrderDate(orderDTO.getOrderDate());
                    order.setOrderType(Order.ORDERTYPE_WEIXIN);
                    order.setOrderStatus(Order.ORDERSTATUS_NO);
                    order.setSetmealId(orderDTO.getSetmealId());
                    orderDao.insert(order);
                    orderSettingDao.updateReservations(orderDate);
                    // 预约成功
                    return new Result(true, MessageConstant.ORDER_SUCCESS, order.getId());
                }else {
                    // 已经完成预约，不能重复预约
                    return new Result(false, MessageConstant.HAS_ORDERED);
                }
            }else {
                // 预约已满
                return new Result(false, MessageConstant.ORDER_FULL);
            }
        }else {
            // 所选日期不能进行体检预约
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
    }

    /**
     * 根据id查询预约
     */
    public OrderInfoVO findById(Integer id) {
        return orderDao.findById(id);
    }
}
